import time
from selenium.webdriver.common.by import By
from common.base import Base
from common.functions import open_browser


class BrowseGoods(Base):
    buy = (By.XPATH, '//*[contains(@src,"themes/default/images/buybtn1.png")]')  # 立即购买定位
    collect = (By.XPATH, '//*[contains(@src,"themes/default/images/bnt_colles.gif")]')  # 收藏定位
    share = (By.XPATH, '//*[contains(@src,"themes/default/images/bnt_recommend.gif")]')  # 分享定位
    tag = (By.ID, 'tag')  # 商品标签框
    add = (By.CLASS_NAME, 'bnt_blue')  # 商品标签添加按钮
    products = (By.XPATH, '//*[@alt="平衡车" and @class="goodsimg"]')  # 平衡车定位
    clothes = (By.LINK_TEXT, '服装')  # 服装定位
    power_bank = (By.LINK_TEXT, '移动电源')  # 移动电源定位
    number = (By.LINK_TEXT, '数码时尚')  # 数码时尚定位
    electrical_appliances = (By.LINK_TEXT, '家用电器')  # 家用电器定位
    previous_page = (By.CLASS_NAME, 'prev')  # 上一页
    next = (By.CLASS_NAME, 'next')  # 下一页

    # 账号
    def username(self, username):
        return self.send_keys((By.NAME, 'username'), username)

    # 密码
    def password(self, password):
        return self.send_keys((By.NAME, 'password'), password)

    # 登录
    def login(self):
        self.click((By.NAME, 'submit'))

    # 退出
    def logout(self):
        self.click((By.LINK_TEXT, '退出'))

    # 首页
    def home_page(self):
        self.click((By.LINK_TEXT, '首页'))

    # 浏览
    def scroll(self):
        self.scroll_low()
        self.scroll_top()

    # 搜索商品
    def search(self):
        self.send_keys((By.ID, "keyword"), '平衡车')
        self.click((By.NAME, 'imageField'))
        time.sleep(1)

    # 点击服装
    def clothe(self):
        self.click(self.clothes)
        time.sleep(1)

    # 点击移动电源
    def bank(self):
        self.click(self.power_bank)
        time.sleep(1)

    # 向下翻页
    def next_page(self):
        self.scroll_low()
        time.sleep(1)
        self.click(self.next)

    #向上翻页
    def prev_page(self):
        self.scroll_low()
        time.sleep(1)
        self.click(self.previous_page)


    # 点击数码时尚
    def numbers(self):
        self.click(self.number)
        time.sleep(2)

    # 点击家用电器
    def electrical(self):
        self.click(self.electrical_appliances)
        time.sleep(1)

    # 点击商品
    def merchandise(self):
        self.scroll_target(self.products)
        self.click(self.products)
        time.sleep(1)

    # 收藏
    def collects(self):
        self.click(self.collect)
        time.sleep(1)
        ale = self.driver.switch_to.alert
        ale.accept()
        self.scroll_top()

    # 立即购买
    def purchase(self):
        self.click(self.buy)
        time.sleep(2)
        self.back()
        time.sleep(1)

    # 分享
    def product(self):
        self.click(self.share)
        time.sleep(1)

    # 商品标签
    def label(self):
        self.send_keys(self.tag, '123')
        self.click(self.add)
        time.sleep(2)
        ale = self.driver.switch_to.alert
        ale.accept()
        time.sleep(2)


if __name__ == '__main__':
    driver = open_browser()
    bp = BrowseGoods(driver)
    bp.get('http://139.129.26.163/user.php')
    bp.username('fine1')
    bp.password('123456')
    bp.login()
    bp.home_page()  # 首页
    bp.scroll()  #浏览
    bp.clothe()  # 服装
    bp.bank()  # 移动电源
    bp.next_page()   #向下翻页
    bp.prev_page()   #向上翻页
    bp.numbers()  # 数码
    bp.electrical()  # 家用电器
    bp.search()  # 搜索
    bp.merchandise()  # 点击商品
    bp.label()  # 商品标签
    bp.collects()  # 收藏
    bp.purchase()  # 购买
    bp.product()  # 分享
    bp.home_page()  #首页
    time.sleep(3)
    bp.quit()
